<!-- src/components/TeacherProfile.vue -->
<template>
  <div class="profile-container">
    <h1>个人信息</h1>
    <el-form :model="userProfile" label-width="100px" class="profile-form">
      <el-form-item label="ID">
        <el-input v-model="userProfile.pkUid" disabled></el-input>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="userProfile.userName" disabled></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="userProfile.realName"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="userProfile.sex" placeholder="请选择性别">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input v-model="userProfile.phone"></el-input>
      </el-form-item>
      <el-form-item label="地址">
        <el-input v-model="userProfile.address"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveProfile">修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: 'Profile',
  data() {
    return {
      userId: localStorage.getItem("userId") || '',
      userProfile: {}
    };
  },

  created(){
    this.fetchUserProfile(this.userId)
  },

  methods: {
    fetchUserProfile(userId){
      axios.get("/api/user/getById?userId="+userId).then(response => {
        this.userProfile = response.data
      }).catch(err => {
        this.$message.error(err.response.data)
      })
    },

    saveProfile() {
      // 处理保存个人信息逻辑
      axios.put("/api/user/update",this.userProfile).then(response => {
        this.$message.success("修改成功")
        this.fetchUserProfile(this.userId)
      }).catch(err => {
          //获取状态码
          const code = err.response.status
          console.log(code)
          if(code === 400){
            this.$message.error("请求参数不能为空")
          }else{
            this.$message.error(err.response.data)
          }
      })
    }
  }
};
</script>

<style scoped>
.profile-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 80vh;
}

.profile-form {
  width: 400px;
}
</style>
